package top.guxx.mall.controller;


import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.guxx.mall.MessageTextEntity;
import top.guxx.mall.SignatureUtil;
import top.guxx.mall.XmlUtil;
import top.guxx.mall.service.ILoginService;

import javax.annotation.Resource;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2024/10/27 23:01
 * @Filename：WeixinPortalController
 */
@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/wechat/portal/")
public class WeChatPortalController {
    @Value("${wechat.config.original-id}")
    private String originalId;

    @Value("${wechat.config.token}")
    private String token;

    @Resource
    private ILoginService loginService;


    @GetMapping(value = "receive", produces = "text/plain;charset=utf-8")
    public String validate(@RequestParam(value = "signature", required = false) String signature,
                           @RequestParam(value = "timestamp", required = false) String timestamp,
                           @RequestParam(value = "nonce", required = false) String nonce,
                           @RequestParam(value = "echostr", required = false) String echostr){
        try {
            log.info("微信公众号验签信息开始 [{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
            if (StringUtils.isAnyBlank(signature,timestamp,nonce,echostr)){
                throw new IllegalArgumentException("方法请求参数，请核实！");
            }
            boolean valid = SignatureUtil.check(token, signature,timestamp,nonce);
            log.info("微信公众号验签完成 check: {}", valid);
            if (!valid){
                return null;
            }
            return echostr;
        }catch (Exception e){
            log.error("微信公众号验签信息失败 [{}, {}, {}, {}]", signature, timestamp, nonce, echostr, e);
            return null;
        }
    }

    @PostMapping(value = "receive", produces = "application/xml; charset=UTF-8")
    public String post(@RequestBody String requestBody,
                       @RequestParam("signature") String signature,
                       @RequestParam("timestamp") String timestamp,
                       @RequestParam("nonce") String nonce,
                       @RequestParam("openid") String openid,
                       @RequestParam(name = "encrypt_type", required = false) String encType,
                       @RequestParam(name = "msg_signature", required = false) String msgSignature){
        try {
            log.info("接收微信公众号信息请求{}开始 {}", openid, requestBody);
            MessageTextEntity message = XmlUtil.xmlToBean(requestBody, MessageTextEntity.class);
            if ("event".equals(message.getMsgType()) && "SCAN".equals(message.getEvent())){
                loginService.saveLoginState(message.getTicket(),openid);
                return buildMessageTextEntity(openid, "登录成功！");
            }
            return buildMessageTextEntity(openid, "登录失败，请重试！");
        }catch (Exception e){
            log.error("接收微信公众号信息请求{}失败 {}", openid, requestBody, e);
            return "";
        }
    }

    private String
    buildMessageTextEntity(String openid, String content) {
        MessageTextEntity res = new MessageTextEntity();
        // 公众号分配的ID
        res.setFromUserName(originalId);
        res.setToUserName(openid);
        res.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000L));
        res.setMsgType("text");
        res.setContent(content);
        return XmlUtil.beanToXml(res);
    }
}
